var seconds = 120;
   const IPaddr = "http://10.141.31.239/Usersmanage"; //服务器IP
//const IPaddr = "http://10.139.23.100:8080/Usersmanage"; //ZFC IP

layui.use('element', function () {
    var element = layui.element;
});
var verifyCode = new GVerify("authCodeImg");
var token = $.cookie("access_token");

$(document).ready(function () {
    $.ajax({
        type: "post",
        url: IPaddr + "/modify/userInfo",
        async: true,
        headers: {"Authorization": token},
        success: function(result){
            var classify = window.location.href.split('updatePhone')[1].split('.html')[0];
            if(classify == "ByMail"){
                document.getElementById('emailAddr').innerHTML = result.data.email;
                ajaxMail = result.data.email;
            }else if(classify == "ByPhone"){
                document.getElementById('phoneAddr').innerHTML = result.data.telephone;
                ajaxPhone = result.data.telephone;
            }else {
                ajaxPhone = result.data.telephone;
                ajaxMail = result.data.email;
            }
        },
        error: function (e) {
            // console.log(e);
        },
    });
})

/*公用函数  开始*/

/*公用函数  结束*/

/*updatePhoneByMail.html内函数  开始*/
function sendMailCode() {
    $.ajax({
        url: IPaddr + "/user/sendVerificationCodeByEmail",
        type: "POST",
        data: {"email": "353226475@qq.com"},
        headers:{"Authorization": token},
        success: function (data) {
            if(data.code == 0){
                document.getElementById('mailText').style.display = 'block';

                document.getElementById('mailCode').disabled = '';

                document.getElementById('sendMailCode').disabled = "disabled";
                document.getElementById('sendMailCode').onclick = "";
                document.getElementById('sendMailCode').style.opacity = "0.2";
                document.getElementById('sendMailCode').style.cursor = "default";
            }else {
                document.getElementById('mailText').innerHTML = '邮件未发送成功，请稍后再试';
                document.getElementById('mailText').style.display = 'block';
            }
        },
        error: function (e) {
            document.getElementById('mailText').innerHTML = '邮件未发送成功，请稍后再试';
            document.getElementById('mailText').style.display = 'block';
        }
    });
}
/*验证码是否输入正确*/
function authCodeCheck() {
    let res = verifyCode.validate(document.getElementById("authCode").value.toLocaleLowerCase());
    let mailCode = document.getElementById('mailCode').value;
    if(res){
        $.ajax({
            url: IPaddr + "/user/verifyVerificationCodeByEmail",
            type: "POST",
            data:{"verificationCode": mailCode, "email": "353226475@qq.com"},
            headers:{"Authorization": "zfc"},
            success: function (data) {
                // console.log(data);
                if(data.code == 0)
                    self.location.href = "updatePhoneStepTwo.html";
                else {
                    document.getElementById('mailError').innerHTML = '邮箱验证码有误，请再次确认';
                    document.getElementById('mailError').style.display = 'block';
                    document.getElementById('mailText').style.display = 'none';
                }
            },
            error: function (e) {
                console.log(e);
            }
        });
    }else{
        document.getElementById('authCodeError').innerHTML = '验证码错误';
        document.getElementById('authCodeError').style.display = 'block';
        document.getElementById("authCode").value = '';
    }
}
/*updatePhoneByMail.html内函数  结束*/




/*点击获取短信校验码时，让链接不可点击并变暗*/
function sendCode() {
    // let phoneNum = document.getElementById('phoneNum').value;
    if(!phoneNumOnblur()){
        document.getElementById('messageCodeError').innerHTML = '请输入正确长度的电话号码'
    }else {
        document.getElementById('messageText').style.display = 'block';
        document.getElementById('messageCode').disabled = '';

        document.getElementById('sendMessage').disabled = "disabled";
        document.getElementById('sendMessage').onclick = "";
        document.getElementById('sendMessage').style.opacity = "0.2";
        document.getElementById('sendMessage').style.cursor = "default";

        counterMinus();
    }
}

/*检查手机校验码的长度*/
function messageCodeOnblur() {
    let val = document.getElementById('messageCode').value;
    if(val == ''|| val == null){
        document.getElementById('messageError').innerHTML = '请输入手机校验码';
        document.getElementById('messageError').style.display = 'block';
        return false;
    }else if(val.length != 6){
        document.getElementById('messageError').innerHTML = '请输入正确长度的手机校验码';
        document.getElementById('messageError').style.display = 'block';
        return false;
    }else {
        return true;
    }
}

function messageCodeOnfocus() {
    document.getElementById('messageError').style.display = 'none';
}

/*倒计时120秒后“获取短信校验码”恢复可点击状态*/
function counterMinus() {
    if(seconds == 0){
        document.getElementById('messageText').style.display = 'none';
        document.getElementById('sendMessage').disabled = "";
        document.getElementById('sendMessage').setAttribute("onclick", "sendCode()");
        document.getElementById('sendMessage').style.opacity = "1";
        document.getElementById('sendMessage').style.cursor = "pointer";
        seconds = 120;
        // document.getElementById('sendMessage').onclick = "";
        return;
    }else {
        document.getElementById('counter').innerHTML = seconds;
        seconds--;
        setTimeout("counterMinus()", 1000);
    }
}

// /*检查email地址是否合法*/
// function checkMailAddr() {
//     document.getElementById('emailText').style.display = 'none';
//     let rightMailAddr = /^\w+@[a-zA-Z0-9]{2,10}(?:\.[a-z]{2,4}){1,3}$/;
//     let mailAddr = document.getElementById('emailAddr').value;
//     if(rightMailAddr.test(mailAddr)){
//         // alert(1);
//         // document.getElementById('emailError').style.display = 'block';
//     }else {
//         document.getElementById('emailError').style.display = 'block';
//     }
// }
//
// function mailAddrOnfocus() {
//     document.getElementById('emailText').style.display = 'block';
//     document.getElementById('emailError').style.display = 'none';
// }
//
// /*提交新邮箱时，检查邮箱和验证码是否正确*/
// function mailCheck() {
//     /*验证验证码是否正确*/
//     let res = verifyCode.validate(document.getElementById("authCode").value.toLocaleLowerCase());
//
//     /*验证邮箱格式是否正确*/
//     let rightMailAddr = /^\w+@[a-zA-Z0-9]{2,10}(?:\.[a-z]{2,4}){1,3}$/;
//     let mailAddr = document.getElementById('emailAddr').value;
//     let mailRight = rightMailAddr.test(mailAddr);
//
//     if(mailRight){
//         if(res){
//             document.getElementById('authCodeError').style.display = 'none';
//             document.getElementById('authCodeError').innerHTML = '';
//             self.location.href = "";
//         }else {
//             document.getElementById('authCodeError').style.display = 'block';
//             document.getElementById('authCodeError').innerHTML = '验证码不正确';
//             // console.log('error');
//         }
//     }else {
//         document.getElementById('emailError').style.display = 'block';
//         if(res){
//             document.getElementById('authCodeError').style.display = 'none';
//             document.getElementById('authCodeError').innerHTML = '';
//         }else {
//             document.getElementById('authCodeError').style.display = 'block';
//             document.getElementById('authCodeError').innerHTML = '验证码不正确';
//         }
//     }
// }

/*验证码input得到焦点时*/
function authCodeFocus() {
    document.getElementById('authCodeError').style.display = 'none';
}

/*手机校验码和验证码检查*/
function phoneCheck() {
    let res = verifyCode.validate(document.getElementById("authCode").value.toLocaleLowerCase());

    if(document.getElementById('messageCode').value.length == 0){
        document.getElementById('textError').innerHTML = '请输入手机校验码';
        document.getElementById('textError').style.display = 'block';
    }else if(document.getElementById('messageCode').value.length != 6){
        document.getElementById('textError').innerHTML = '请输入正确长度的手机校验码';
        document.getElementById('textError').style.display = 'block';
    }else {
        if(res){
            // alert("验证正确");
            document.getElementById('authCodeError').style.display = 'none';
            self.location.href = "";
        }else{
            document.getElementById("authCode").value = '';
            document.getElementById('authCodeError').innerHTML = '验证码错误';
            document.getElementById('authCodeError').style.display = 'block';
        }
    }
}


/*检查修改登陆的两次结果是否一致和验证码是否正确*/
function checkPassword() {
    let res = verifyCode.validate(document.getElementById("authCode").value.toLocaleLowerCase());
    if(confirmPasswordOnblur() && passwordOnblur()){
        // console.log(confirmPasswordOnblur());
        if(res){
            document.getElementById('authCodeError').style.display = 'none';
            console.log("验证码和密码输入正确");
        }else {
            document.getElementById('authCodeError').innerHTML = '验证码错误';
            document.getElementById('authCodeError').style.display = 'block';
            document.getElementById("authCode").value = '';
        }
    }else {
    }
}

/*密码输入提示*/
function passwordOnfocus() {
    document.getElementById('passwordMessage').style.display = 'block';
    document.getElementById('passwordError').style.display = 'none';
}

function passwordOnblur() {
    document.getElementById('passwordMessage').style.display = 'none';
    let val = document.getElementById('password').value;
    if(val.length < 6){
        document.getElementById('passwordError').innerHTML = '密码长度不正确，请重新输入';
        document.getElementById('passwordError').style.display = 'block';
        return false;
    }else {
        let modes = 0;
        if (/\d/.test(val)) modes++; //数字
        if (/[a-z]/.test(val)) modes++; //小写
        if (/[A-Z]/.test(val)) modes++; //大写
        if (/\W/.test(val)) modes++; //特殊字符

        if(modes < 3){
            document.getElementById('passwordError').innerHTML = '密码强度弱，请重新输入';
            document.getElementById('passwordError').style.display = 'block';
            return false;
        }else
            return true;
    }
    // console.log(val);
}

/*检查两次密码是否一致*/
function confirmPasswordOnblur() {
    let password = document.getElementById('password').value;
    let confirmPassword = document.getElementById('confirmPassword').value;
    if(password != confirmPassword){
        document.getElementById('confirmPasswordError').style.display = 'block';
        return false;
    }else {
        document.getElementById('confirmPasswordError').style.display = 'none';
        return true;
    }
}

function confirmPasswordFocus() {
    document.getElementById('confirmPasswordError').style.display = 'none';
}

/**/
function phoneNumFocus() {
    document.getElementById('messageCodeError').style.display = 'none';
}

/*检查电话号码的长度*/
function phoneNumOnblur() {
    if(document.getElementById('phoneNum').value.length != 11) {
        document.getElementById('messageCodeError').innerHTML = '电话号码长度不正确';
        document.getElementById('messageCodeError').style.display = 'block';
        return false;
    }else {
        document.getElementById('messageCodeError').style.display = 'none';
        return true;
    }
}


/*检查修改手机号码时新手机号码和验证码是否正确*/
function checkPhoneNum() {
    let res = verifyCode.validate(document.getElementById("authCode").value.toLocaleLowerCase());
    if(phoneNumOnblur() && messageCodeOnblur()){
        if(res){
            document.getElementById('authCodeError').style.display = 'none';
            console.log(123);
        }else {
            document.getElementById("authCode").value = '';
            document.getElementById('authCodeError').innerHTML = '验证码错误';
            document.getElementById('authCodeError').style.display = 'block';
        }
    }
}
